package com.yanq.liancar.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.Indexlist.SectionDecoration;
import com.yanq.liancar.R;
import com.yanq.liancar.adapter.CarCheXiAdapter;
import com.yanq.liancar.adapter.CarCheXingAdapter;
import com.yanq.liancar.adapter.CarRelationAdapter;
import com.yanq.liancar.bean.CarPinpaiBean;
import com.yanq.liancar.bean.CheXiBean;
import com.yanq.liancar.bean.ChexingBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.LogUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SelectCarBrandActivity extends Activity{
    private RecyclerView mRecyclerView;
    private List<CarPinpaiBean.DataBean> mPaipaiList = new ArrayList<>();
    private List<CheXiBean.DataBean> mChexiList=new ArrayList<>();
    private List<ChexingBean.DataBean> mChexingList=new ArrayList<>();
    private CarRelationAdapter mCarRelationAdapter;
    private CarCheXiAdapter mCarCheXiAdapter;
    private CarCheXingAdapter mCarCheXingAdapter;
    private int where,pinpaiID,chexiID;
    private TextView tv_title;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_car_brand);
        tv_title=findViewById(R.id.tv_title);
        where=getIntent().getIntExtra("where",0);

        initRecyclerview();
        if (where==1){
            initOkGo();
            tv_title.setText("请选择车辆品牌");
        }else if (where==2){
            tv_title.setText("请选择车辆车系");
            pinpaiID=getIntent().getIntExtra("pinpaiID",0);
            initOkGo1();
        }else {
            tv_title.setText("请选择车辆车型");
            chexiID=getIntent().getIntExtra("chexiID",0);
            initOkGo2();
        }
    }

    private void initRecyclerview() {
        mRecyclerView = (RecyclerView) findViewById(R.id.carbrand_list);
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//        mRecyclerView.addItemDecoration(new RecycleViewDivider(
//                getApplicationContext(), LinearLayoutManager.HORIZONTAL, 2, getResources().getColor(R.color.select_addr_jiange)));
        if (where==1){
            mRecyclerView.addItemDecoration(new SectionDecoration(this, new SectionDecoration.DecorationCallback() {
                @Override
                public long getGroupId(int position) {
                    return  Character.toUpperCase(mPaipaiList.get(position).getCar_pinyin().charAt(0));
                }

                @Override
                public String getGroupFirstLine(int position) {
                    return mPaipaiList.get(position).getCar_pinyin().substring(0, 1).toUpperCase();
                }
            }));
        }
    }

    //查询所有品牌
    private void initOkGo() {
        OkGo.<String>post(Config.s + "llc/car/pinpai")
                .tag(this)
                // .params("car_series_id", UserBean.getcToken(mContext))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        CarPinpaiBean bean = gson.fromJson(response.body(), CarPinpaiBean.class);
                        if (bean.isIsSuccess()) {
                            mPaipaiList.clear();
                            mPaipaiList.addAll(bean.getData());
                            Collections.sort(mPaipaiList);
                            mCarRelationAdapter=new CarRelationAdapter(R.layout.item_carbrand,mPaipaiList);
                            mRecyclerView.setAdapter(mCarRelationAdapter);
                            mCarRelationAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                @Override
                                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                    Intent intent=new Intent();
                                    intent.putExtra("pinpaiID",mPaipaiList.get(position).getCar_brand_id());
                                    intent.putExtra("pinpaiName",mPaipaiList.get(position).getCar_trademark());
                                    setResult(RESULT_OK,intent);
                                    finish();
                                }
                            });
                        }
                    }
                });
    }

    //查询所有车系
    private void initOkGo1() {
        OkGo.<String>post(Config.s + "llc/car/carxi")
                .tag(this)
                .params("car_brand_id", pinpaiID)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        CheXiBean bean = gson.fromJson(response.body(), CheXiBean.class);
                        if (bean.isIsSuccess()) {
                            if (bean.getCode()==200){
                                mChexiList.clear();
                                mChexiList.addAll(bean.getData());

                                mCarCheXiAdapter=new CarCheXiAdapter(R.layout.item_carbrand_1,mChexiList);
                                mRecyclerView.setAdapter(mCarCheXiAdapter);
                                mCarCheXiAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                        Intent intent=new Intent();
                                        intent.putExtra("chexiID",mChexiList.get(position).getCar_series_id());
                                        intent.putExtra("chexiName",mChexiList.get(position).getCar_series());
                                        setResult(RESULT_OK,intent);
                                        finish();
                                    }
                                });
                            }else {
                                RxToast.normal(bean.getOutputMessage());
                            }

                        }else {
                            RxToast.error(bean.getOutputMessage());
                        }
                    }
                });
    }

    //查询所有车型
    private void initOkGo2() {
        OkGo.<String>post(Config.s + "llc/car/carmodel")
                .tag(this)
                .params("car_series_id", chexiID)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        ChexingBean bean = gson.fromJson(response.body(), ChexingBean.class);
                        if (bean.isIsSuccess()) {
                            if (bean.getCode()==200){
                                mChexingList.clear();
                                mChexingList.addAll(bean.getData());

                                mCarCheXingAdapter=new CarCheXingAdapter(R.layout.item_carbrand_1,mChexingList);
                                LogUtils.e("asdadadasdas",mChexingList.size()+"");
                                mRecyclerView.setAdapter(mCarCheXingAdapter);
                                mCarCheXingAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                        Intent intent=new Intent();
                                        intent.putExtra("chexingID",mChexingList.get(position).getCar_model_id());
                                        intent.putExtra("chexing",mChexingList.get(position).getCar_model());
                                        setResult(RESULT_OK,intent);
                                        finish();
                                    }
                                });
                            }else {
                                RxToast.normal(bean.getOutputMessage());
                            }
                        }else {
                            RxToast.error(bean.getOutputMessage());
                        }
                    }
                });
    }


}
